﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlackBird.Models;
using System.IO;
using BlackBird.ControllerHelpers;
using BlackBird.DataAccess;

namespace BlackBird.Controllers
{
	//收入
	public partial class IncomeExpendController : Controller
    {
        //
        // GET: /Income/

		public virtual ActionResult Index()
        {
            return View();
        }

		[HttpGet]
		public virtual ActionResult AddIncomeAndExpend(string ParentId,SupportTypes type = SupportTypes.Income)
		{
			IncomeAndExpendInputDM dm = new IncomeAndExpendInputDM()
			{
				ParentId = ParentId,
				type = type
			};

			if (type == SupportTypes.Income)
				ViewBag.Title = "输入收入信息";
			else
				ViewBag.Title = "输入支出信息";
			

			return PartialView(MVC.Shared.Views.IncomeAndExpend.AddIncomeExpend, dm);
		}

		[HttpPost]
		public virtual ActionResult AddIncomeAndExpend(IncomeAndExpendInputDM dm)
		{
			var currentuserid = AccountHelper.GetCurrentUserId(this);
			dm.Status = SupportTypeStatus.CaoGao;
			dm.Date = DateTime.Now;
			int eventid = EventsDataAccess.InsertNewEvent(dm, currentuserid);
			dm.Id = eventid.ToString();

			foreach (string file in Request.Files)
			{
				HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
				if (hpf.ContentLength == 0)
					continue;

				var savedFilePath = FileUploadHelper.SaveUploadFile(hpf, dm.type);

				var currentUser = AccountHelper.GetCurrentUser(this);

				//Insert Fu Jian Info to fujian table.
				int fujianID = FuJianDataAccess.SaveFuJianItemToDB(Path.GetFileName(hpf.FileName), savedFilePath, hpf.ContentType, currentUser.idUser);

				//Update fujian parent table info.        
				EventsDataAccess.InsertEvent_to_Fujian(eventid, fujianID);
			}
			//for test
			return Json(dm, JsonRequestBehavior.AllowGet);
		}
		

    }
}
